﻿using System;
using System.Collections.Generic;

public class MsgHandler
{
    /// <summary>
    /// 获取信息
    /// </summary>
    /// <param name="guid"></param>
    /// <param name="msgBase"></param>
    public static void MsgGetInfo(uint guid, MsgBase msgBase)
    {
        MsgGetInfo msg = (MsgGetInfo)msgBase;
        msg.guid = guid;



        NetManager.Send(msgBase, guid);
    }
    public static void MsgLockStep(uint guid, MsgBase msgBase)
    {
        MsgLockStep msg = (MsgLockStep)msgBase;
        Player player = PlayerManager.GetPlayer(guid);

        if (player == null)
            return;
        MatchRoom room = MatchRoomManager.GetRoom(player.roomId);
        if (room == null)
            return;
        Console.WriteLine(room.lockStepManager.allOpt.Count);
        LockStepManager lockStepManager = room.lockStepManager;
        lock (lockStepManager)
        {
            if (!lockStepManager.allOpt.ContainsKey(msg.turn))
            {
                lockStepManager.allOpt.Add(msg.turn, new List<Opts>());
            }
            for (int i = 0; i < msg.opts.Length; i++)
            {
                lockStepManager.allOpt[msg.turn].Add(msg.opts[i]);
            }
            room.playerUnsyncOpt[guid] = msg.turn - 1;
        }
    }
    public static void MsgMatchPlayerReq(uint guid, MsgBase msgBase) 
    {
        MsgMatchPlayerRsp msgMatchPlayerRsp=new MsgMatchPlayerRsp();
        MsgMatchPlayerReq msgMatchPlayerReq=(MsgMatchPlayerReq)msgBase;
        if (PlayerManager.GetPlayer(guid)==null)
        {
            Player player = new Player(guid);
            PlayerManager.AddPlayer(guid, player);
            PlayerManager.Add(msgMatchPlayerReq.username, player);
        }
        uint roomCount = 0;
        foreach (var room in MatchRoomManager.matchRooms)
        {
            if (room.Value.currentPlayerCount==1)
            {
                room.Value.currentPlayerCount++;
                room.Value.AddPlayer(guid);
                room.Value.Add(msgMatchPlayerReq.username);
                room.Value.StartLockStep();
                msgMatchPlayerRsp.guids = new uint[room.Value.playerIds.Count];
                msgMatchPlayerRsp.usernames = new string[room.Value.playerUsernames.Count];
                for (int i = 0; i < room.Value.playerIds.Count; i++)
                {
                    Console.WriteLine("房间里的username等于："+room.Value.playerUsernames[i]+"    guid等于：" + room.Value.playerIds[i]);
                    msgMatchPlayerRsp.guids[i] = room.Value.playerIds[i];
                    msgMatchPlayerRsp.usernames[i] = room.Value.playerUsernames[i];
                }
                for (int i = 0; i < room.Value.playerIds.Count; i++)
                {
                    NetManager.Send(msgMatchPlayerRsp, msgMatchPlayerRsp.guids[i]);
                }
                break;
            }
            else
            {
                roomCount++;
            }
        }
        if (roomCount==MatchRoomManager.matchRooms.Count)
        {
            MatchRoom room=MatchRoomManager.AddRoom();
            room.currentPlayerCount++;
            room.AddPlayer(guid);
            room.Add(msgMatchPlayerReq.username);
        }
    }
}

